package com.uzai.console.service.cms.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.cms.base.CmsBaseInfoDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.cms.CmsBaseService;
import com.uzai.console.vo.cms.base.CmsBaseInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class CmsBaseServiceImpl implements CmsBaseService {

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询CMS基础设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public CmsBaseInfoVo findCmsBaseInfo(Long merId) {

        CmsBaseInfoVo cmsBaseInfoVo = new CmsBaseInfoVo();

        //查询运营商CMS基础设置信息
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.CMS_BASE_CONFIG.getValue());
        if (merBaseConfig != null) {
            cmsBaseInfoVo = JSONObject.parseObject(merBaseConfig.getVal(), CmsBaseInfoVo.class);
        }
        if (Objects.isNull(cmsBaseInfoVo.getShowBalance())) {
            cmsBaseInfoVo.setShowBalance(0);
        }
        return cmsBaseInfoVo;
    }

    /**
     * 修改CMS基础设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param cmsBaseInfoDto
     * @return null
     */
    public void updateCmsBase(CmsBaseInfoDto cmsBaseInfoDto){

        if(StringUtils.isBlank(cmsBaseInfoDto.getMallName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入商城名称");
        }

        CmsBaseInfoVo cmsBaseInfoVo = new CmsBaseInfoVo();
        BeanUtils.copyProperties(cmsBaseInfoDto, cmsBaseInfoVo);

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(cmsBaseInfoDto.getMerId(), MerConfigKeyEnum.CMS_BASE_CONFIG.getValue());

        Integer now = DateUtil.getNowTime();
        //修改
        if (merBaseConfig != null){
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(cmsBaseInfoVo)));
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        } else { //增加
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(cmsBaseInfoDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.CMS_BASE_CONFIG.getValue());
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(cmsBaseInfoVo)));
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }

        //保存到Redis当中
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.CMS_BASE_CONFIG.getKey(), cmsBaseInfoDto.getMerId()), merBaseConfig.getVal());
    }

}
